package com.zz.cc.owner.app;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.zz.cc.owner.R;
import com.zz.cc.owner.data.Store;
import com.zz.cc.owner.util.Util;

//店铺列表界面
public class StoreListActivity extends OwnerBaseActivity {
	private ListView mListView;
	private MyAdapter mAdapter;
	private List<Store> mStores = new ArrayList<Store>();
	private Button mRightBtnButton;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		emulateData();
		initUI();
	}
	private void emulateData(){
		Store store1 = new Store();
		store1.name = "天利洗车店";
		store1.originalPrice = 20.0f;
		store1.preferencialPrice = 15.0f;
		store1.introduce="我们天利洗车店开了20年了，深受各位车友的喜爱";
		store1.serverId = 1;
		store1.id = 1;
		store1.score = 4.5f;
		store1.address = "深圳市南山区南新路118号";
		store1.phone = "13388948734";
		mStores.add(store1);
		
		Store store2 = new Store();
		store2.name = "小明洗车店";
		store2.originalPrice = 20.0f;
		store2.preferencialPrice = 10.0f;
		store2.introduce="我们小明洗车店是最好的";
		store2.serverId = 2;
		store2.id = 2;
		store2.score = 2.0f;
		store2.address = "深圳市罗湖区春风路10号";
		store2.phone = "13788946512";
		mStores.add(store2);
	}
	private void initUI(){
		setContentView(R.layout.storelist);
		((TextView)findViewById(R.id.titleTextView)).setText("我身边的洗车店");
		findViewById(R.id.backImg).setVisibility(View.GONE);
		mRightBtnButton = (Button)findViewById(R.id.rightBtn);
		mRightBtnButton.setText("地图模式");
		mRightBtnButton.setVisibility(View.VISIBLE);
		mRightBtnButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Util.showToast(StoreListActivity.this, "跳转到地图模式");
				finish();
			}
		});
		mListView = (ListView)findViewById(R.id.storelist);
		mAdapter = new MyAdapter(this);
		mListView.setAdapter(mAdapter);
		mListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent intent = new Intent(StoreListActivity.this, DetailActivity.class);
				intent.putExtra(DetailActivity.STORE_DETAIL, (Store)mAdapter.getItem(arg2));
				startActivity(intent);
			}
		});
	}
	
	class MyAdapter extends BaseAdapter{
		private Context ctx;
		public MyAdapter(Context context){
			this.ctx = context;
		}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mStores.size();
		}
		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return mStores.get(position);
		}
		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder viewHolder = null;
			Store store = (Store)getItem(position);
			if (convertView==null) {
				convertView = LayoutInflater.from(this.ctx).inflate(R.layout.storelist_item,null);
				viewHolder = new ViewHolder();
				viewHolder.logoView = (ImageView)convertView.findViewById(R.id.logo_img);
				viewHolder.distance = (TextView)convertView.findViewById(R.id.distance);
				viewHolder.price = (TextView)convertView.findViewById(R.id.price);
				viewHolder.name = (TextView)convertView.findViewById(R.id.name);
				viewHolder.position = (TextView)convertView.findViewById(R.id.position);
				convertView.setTag(viewHolder);
			}else {
				viewHolder = (ViewHolder)convertView.getTag();
			}
			viewHolder.name.setText(store.name);
			viewHolder.logoView.setImageResource(R.drawable.ic_launcher);
			viewHolder.distance.setText(((position+1)*100)+"米");
			viewHolder.price.setText(store.preferencialPrice+"元");
			viewHolder.position.setText(store.address);
			return convertView;
		}
		class ViewHolder{
			ImageView logoView;
			TextView name;
			TextView distance;
			TextView price;
			TextView position;
		}
	}
}
